package com.augurit.gzcgwsp.common.dwr;

import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.augurit.common.util.lang.StringUtils;

public class BaseDwrService {
    private Logger logger = LoggerFactory.getLogger(BaseDwrService.class);

    public String getJsonString(final Object object) {
        String jsonString = JSONObject.fromObject(object).toString();
        return jsonString;
    }
    
    public String getJsonString(final Map map) {
        String jsonString = JSONObject.fromObject(map).toString();
        return jsonString;
    }
    
    public Object toBeanFromJson(final String beanJsonString, final Class beanClass){
        if(StringUtils.isNotBlank(beanJsonString)){
            JSONObject jsonObject = JSONObject.fromObject(beanJsonString);
            return JSONObject.toBean(jsonObject, beanClass);
        }else
            return null;
    }
    
    public Object[] toBeanListFromJson(final String multiBeanJsonString, final Class beanClass){
        if(StringUtils.isNotBlank(multiBeanJsonString)){
            StringBuffer sb = new StringBuffer("[");
            sb.append(multiBeanJsonString.replace("}{", "},{"));
            sb.append("]");
            JSONArray jsonArray = JSONArray.fromObject(sb.toString());
            return (Object[])JSONArray.toArray(jsonArray, beanClass);
        }else
            return null;
    }
}
